<template>
	<view style="padding: 20rpx;">
		<uni-forms ref="form" :rules="rules" :model="formData" validateTrigger="blur" :labelWidth="100"
			labelAlign="right">
			<uni-forms-item name="oldPassword" :required="true" label="原密码">
				<uni-easyinput v-model="formData.oldPassword" :clearable="true" type="password"
					placeholder="请输入6-20位原密码"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="newPassword" :required="true" label="新密码">
				<uni-easyinput v-model="formData.newPassword" :clearable="true" type="password"
					placeholder="请输入6-20位新密码"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="repeatNewPassword" :required="true" label="重复新密码">
				<uni-easyinput v-model="formData.repeatNewPassword" :clearable="true" type="password"
					placeholder="请重复新密码"></uni-easyinput>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	import httpRequest from '@/utils/request';
	import {
		userStore
	} from "@/store/user.js";

	export default {
		data() {
			return {
				formData: {
					oldPassword: "",
					newPassword: "",
					repeatNewPassword: ""
				},
				rules: {
					oldPassword: {
						rules: [{
								required: true,
								errorMessage: "请输入原密码"
							},
							{
								minLength: 6,
								maxLength: 20,
								errorMessage: "原密码应为6-20位"
							}
						]
					},
					newPassword: {
						rules: [{
								required: true,
								errorMessage: "请输入新密码"
							},
							{
								minLength: 6,
								maxLength: 20,
								errorMessage: "新密码应为6-20位"
							}
						]
					},
					repeatNewPassword: {
						rules: [{
								required: true,
								errorMessage: "请重复新密码"
							},
							{
								minLength: 6,
								maxLength: 20,
								errorMessage: "新密码应为6-20位"
							},
							{
								validateFunction: (rule, value, data, callback) => {
									if (this.formData.newPassword !== value) {
										callback("两次输入密码不一致");
									}
									return true;
								}
							}
						]
					}
				}
			};
		},
		onNavigationBarButtonTap() {
			const useUserStore = userStore()
			this.$refs.form.validate().then(() => {
				httpRequest({
					method: "PUT",
					url: "/user/changePassword",
					data: this.formData
				}).then(res => {
					uni.showToast({
						icon: 'success',
						title: res.message,
					})
					useUserStore.destroyToken();
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/login"
						})
					}, 1000)
					console.log(res);
				})
			})
		}
	}
</script>

<style lang="scss">

</style>
